$(function () {
    findById();
});

/**
 * 查询id
 */
function findById() {
    let id = sessionStorage.getItem("companyId");
    let result = myAjax("/admin/company/findById", {id: id},"post");
    console.log(result);
    if (result != null) {
        setData(result.model);
    }

}

function setData(data) {
    $("#companyName").val(data.name);
    $("#showImg").attr("src",data.logo);
    edit.txt.html(data.content)
   $("#phone").val(data.phone);
   $("#email").val(data.email);
   $("#showQrCodeIndex").attr("src",data.qrCodeIndex);
   $("#showQrCodeWeiXin").attr("src",data.qrCodeWeiXin);
   $("#address").val(data.address);
   $("#online").val(data.online);
   $("#recordNumber").val(data.recordNumber);
   $("#recordMessage").val(data.recordMessage);
   $("input[name='status'][value=" + data.status + "]").attr("checked", true);
}


//返回用户查询结果页面
function goBack() {
    $('.right').load('/html/admin/company/companyManage.html');
}
//提交表单时再一次判断提交数据是否正确
function save() {
    let data = new FormData();
    data.append("id",sessionStorage.getItem("companyId"))
    data.append("name", $("#companyName").val());
    data.append("online", $("#online").val());
    data.append("address", $("#address").val());
    data.append("phone", $("#phone").val());
    data.append("email", $("#email").val());
    data.append("status", $("input:radio[name='status']:checked").val());
    //拿到的图片有可能是一组数据，[0].files[0]选中当前对象的第一个数据
    data.append("img", $("#logo")[0].files[0]);
    data.append("qrCodeIndex", $("#qrCodeIndex")[0].files[0]);
    data.append("qrCodeWeiXin", $("#qrCodeWeiXin")[0].files[0]);
    data.append("recordMessage", $("#recordMessage").val());
    data.append("recordNumber", $("#recordNumber").val());
    data.append("content",edit.txt.html());

    let result = myImgAjax("/adminFile/company/update", data,"post");
    console.log(result);
    if(result.result == "success"){
        alert("修改成功（== ^^ ==）！")
        $('.right').load('/html/admin/company/companyManage.html');
    }else if(result.result == "noLogin"){
        window.open("/html/login.html","_self");
    }else{
        alert("修改失败！");
    }
}